<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>人员任务统计</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .layui-form-label {
            width: 100px;
        }
        .layui-input-inline {
            width: 150px;
        }
        .layui-table-cell {
            text-align: center;
        }
        .month-column {
            min-width: 70px;
        }
        .total-column {
            min-width: 90px;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">年份</label>
                            <div class="layui-input-inline">
                                <select name="year" id="year" lay-filter="year">
                                    <option value="">请选择年份</option>
                                    <!-- 动态填充年份 -->
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">人员</label>
                            <div class="layui-input-inline">
                                <select name="empName" id="empName">
                                    <option value="">请选择人员</option>
                                    <!-- 动态填充人员 -->
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"
                                    lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon">&#xe615;</i> 查询
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="personTaskStatisticsTable" lay-filter="personTaskStatisticsFilter"></table>

        <script type="text/html" id="tableToolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="export">导出Excel</button>
            </div>
        </script>
    </div>
</div>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'jquery'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        // 初始化下拉框数据（年份和人员）
        $.ajax({
            url: '/statistics/personTask/selectOptions',
            type: 'GET',
            success: function (res) {
                // 填充年份下拉框
                var yearHtml = '<option value="">请选择年份</option>';
                $.each(res.years, function (index, item) {
                    yearHtml += '<option value="' + item + '">' + item + '</option>';
                });
                $('#year').html(yearHtml);
                form.render('select');

                // 填充人员下拉框
                var empHtml = '<option value="">请选择人员</option>';
                $.each(res.empNames, function (index, item) {
                    empHtml += '<option value="' + item + '">' + item + '</option>';
                });
                $('#empName').html(empHtml);
                form.render('select');
            }
        });

        // 初始化表格
        table.render({
            elem: '#personTaskStatisticsTable',
            url: '/statistics/personTask/monthly',
            loading: true,
            toolbar: '#tableToolbar',
            parseData: function(res) {
                return {
                    "code": 0,
                    "msg": "",
                    "count": res.length,
                    "data": res
                };
            },
            cols: [[
                {field: 'empName', title: '人员', width: 120, align: 'center', sort: true, fixed: 'left'},
                {field: 'january', title: '1月', width: 80, align: 'center', class: 'month-column'},
                {field: 'february', title: '2月', width: 80, align: 'center', class: 'month-column'},
                {field: 'march', title: '3月', width: 80, align: 'center', class: 'month-column'},
                {field: 'april', title: '4月', width: 80, align: 'center', class: 'month-column'},
                {field: 'may', title: '5月', width: 80, align: 'center', class: 'month-column'},
                {field: 'june', title: '6月', width: 80, align: 'center', class: 'month-column'},
                {field: 'july', title: '7月', width: 80, align: 'center', class: 'month-column'},
                {field: 'august', title: '8月', width: 80, align: 'center', class: 'month-column'},
                {field: 'september', title: '9月', width: 80, align: 'center', class: 'month-column'},
                {field: 'october', title: '10月', width: 80, align: 'center', class: 'month-column'},
                {field: 'november', title: '11月', width: 80, align: 'center', class: 'month-column'},
                {field: 'december', title: '12月', width: 80, align: 'center', class: 'month-column'},
                {field: 'total', title: '合计', width: 100, align: 'center', class: 'total-column', sort: true}
            ]],
            page: false, // 不分页
            skin: 'line'
        });

        // 搜索功能
        form.on('submit(data-search-btn)', function (data) {
            var year = $('#year').val();
            var empName = $('#empName').val();

            table.reload('personTaskStatisticsTable', {
                url: '/statistics/personTask/monthly',
                where: {
                    year: year,
                    empName: empName
                }
            });
            return false;
        });

        // 工具栏事件
        table.on('toolbar(personTaskStatisticsFilter)', function (obj) {
            if (obj.event === 'export') {
                var year = $('#year').val();
                var empName = $('#empName').val();

                var url = '/statistics/personTask/export?' +
                    'year=' + encodeURIComponent(year || '') +
                    '&empName=' + encodeURIComponent(empName || '');

                window.location.href = url;
            }
        });
    });
</script>
</body>
</html>